package com.cbg.sys.controller;

import com.cbg.base.constant.Constant;
import com.cbg.base.utils.Result;
import com.cbg.sys.entity.Line;
import com.cbg.sys.service.ILineService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.cbg.base.core.SuperController;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.IntStream;

/**
 * <p>
 * 排班表 前端控制器
 * </p>
 *
 * @author chenbing
 * @since 2018-12-13
 */
@RestController
@RequestMapping("/sys/line")
@Api(value = "班次控制器",tags = "班次操作接口")
public class LineController extends SuperController<ILineService, Line> {
    @Autowired
    RedisTemplate<String,Line> lineRedisTemplate;

    @ApiOperation(value="展示班次", notes="展示班次")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "routeId", value = "路线ID", required = true, dataType = "String",paramType = "query")
            //@ApiImplicitParam(name = "dateSign", value = "日期标识", required = true, dataType = "Integer")
    })
    @GetMapping("/all")
    public Result<?> lines(String routeId){
        List<Line> lines=new ArrayList<>();
        IntStream.rangeClosed(0,2).forEach(x->{
            List<Line> range = lineRedisTemplate.opsForList()
                    .range(Constant.LINE_SUFFIX + routeId + ":" + x, Constant.LIST_START, Constant.LIST_END);
            if(Optional.ofNullable(range).isPresent())
                lines.addAll(range);
        });
        return Result.ok(lines);
    }
}
